package com.medicine.hbp.adapter.self;

import java.util.List;

import org.kymjs.kjframe.KJBitmap;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.widget.RoundImageView;

import com.medicine.hbp.adapter.BtqianAdapter;
import com.medicine.hbp.base.MyBaseAdapter;
import com.medicine.hbp.emotion.StringUtils;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.ArtInfoVo;
import com.medicine.hbp.mob.artGroupVo;
import com.medicine.hbp.ui.WebActivity;
import com.medicine.hbp.uitl.Share;
import com.medicine.hbp.uitl.StartIntent;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class SelfUserScAdapter extends MyBaseAdapter {
	private LayoutInflater inflater;
	private KJBitmap bitmap = new KJBitmap();
	public SelfUserScAdapter(Activity activity) {
		this.activity=activity;
		inflater=LayoutInflater.from(activity);
	}

	@Override
	public View getView(int position, View view, ViewGroup arg2) {
		ViewHolder holder;
		if (view == null) {
			holder = new ViewHolder();
			view = inflater.inflate(R.layout.item_classification_detail, null);
//			holder.text_ification_username = (TextView) view.findViewById(R.id.text_ification_username);
//			holder.text_ification_detail = (TextView) view.findViewById(R.id.text_ification_detail);
//			holder.text_pl_detail = (TextView) view.findViewById(R.id.text_pl_detail);
//			holder.text_wenz_zan = (TextView) view.findViewById(R.id.text_wenz_zan);
//			holder.text_wenz_bi = (TextView) view.findViewById(R.id.text_wenz_bi);
//			holder.text_wenz_re = (TextView) view.findViewById(R.id.text_wenz_re);
//			holder.text_pl_zan = (TextView) view.findViewById(R.id.text_pl_zan);
//			holder.text_discussUser = (TextView) view.findViewById(R.id.text_discussUser);
//
//			holder.ll_wenz_zan = (LinearLayout) view.findViewById(R.id.ll_wenz_zan);
//			holder.ll_wenz_bi = (LinearLayout) view.findViewById(R.id.ll_wenz_bi);
//			holder.ll_wenz_re = (LinearLayout) view.findViewById(R.id.ll_wenz_re);
//			holder.ll_pl_zan = (LinearLayout) view.findViewById(R.id.ll_pl_zan);
//
//			holder.img_classif_one = (ImageView) view.findViewById(R.id.img_classif_one);
//			holder.img_classif_two = (ImageView) view.findViewById(R.id.img_classif_two);
//			holder.img_shoucang = (ImageView) view.findViewById(R.id.img_shoucang);
//			holder.img_zan_no = (ImageView) view.findViewById(R.id.img_zan_no);
//			holder.img_bishi_no = (ImageView) view.findViewById(R.id.img_bishi_no);
//			holder.img_share = (ImageView) view.findViewById(R.id.img_share);
//			
//			holder.ll_class_usert= (LinearLayout) view.findViewById(R.id.ll_class_usert);
//			holder.ll_class_user = (LinearLayout) view.findViewById(R.id.ll_class_user);
//			holder.btn_exit = (Button) view.findViewById(R.id.btn_exit);
//			holder.grid_bqtype = (GridView) view.findViewById(R.id.grid_bqtype);
//			
//			
//			holder.img_userhead01 = (RoundImageView) view.findViewById(R.id.text_ification_userhead);
//			holder.img_userhead02 = (RoundImageView) view.findViewById(R.id.text_discussUserhead);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		final ArtInfoVo groupVo = (ArtInfoVo) getItem(position);
		if (groupVo != null) {
//			holder.text_ification_username.setText(groupVo.artUser.userName);
			holder.text_ification_detail.setText(StringUtils.getEmotionContent(
					activity, holder.text_ification_detail, groupVo.artMsg));
			holder.text_pl_detail.setText(groupVo.discussNote);
			holder.text_wenz_zan.setText(groupVo.artPraiseNumber.toString());
			holder.text_wenz_bi.setText(groupVo.artNegativeNumber.toString());
			holder.text_wenz_re.setText(groupVo.artDiscussNumber.toString());
			bitmap.display(holder.img_userhead01, groupVo.artUser.userHeadImg);
			bitmap.display(holder.img_userhead02, groupVo.discussUser.userHeadImg);
			if (groupVo.isCollect!=null&&groupVo.isCollect) {
				holder.img_shoucang.setImageDrawable(activity.getResources().getDrawable(R.mipmap.img_collection_yes));
			}else{
				holder.img_shoucang.setImageDrawable(activity.getResources().getDrawable(R.mipmap.img_collection_no));
			}
			if (groupVo.isArtPraiseNumber!=null&&groupVo.isArtPraiseNumber) {//赞
				holder.img_zan_no.setImageDrawable(activity.getResources().getDrawable(R.mipmap.img_zan_yes));
			}else{
				holder.img_zan_no.setImageDrawable(activity.getResources().getDrawable(R.mipmap.img_zan_no));
			}
			if (groupVo.isArtNegativeNumber!=null&&groupVo.isArtNegativeNumber) {//差评
				holder.img_bishi_no.setImageDrawable(activity.getResources().getDrawable(R.mipmap.img_bishi_yes));
			}else{
				holder.img_bishi_no.setImageDrawable(activity.getResources().getDrawable(R.mipmap.img_bishi_no));
			}
			if (groupVo.artImg.size() > 0) {
				if (!TextUtils.isEmpty(groupVo.artImg.get(0).artImgUrl)) {
					bitmap.display(holder.img_classif_one,groupVo.artImg.get(0).artImgUrl);
				}
			}
			if (groupVo.artImg.size() > 1) {
				if (!TextUtils.isEmpty(groupVo.artImg.get(1).artImgUrl)) {
					bitmap.display(holder.img_classif_two,groupVo.artImg.get(1).artImgUrl);
				}
			}
			if(groupVo.artImg.size()==0){
				holder.img_classif_one.setVisibility(View.GONE);
				holder.img_classif_two.setVisibility(View.GONE);
			}else if(groupVo.artImg.size()==1){
				holder.img_classif_one.setLayoutParams(new LayoutParams(view.getWidth()-20, view.getWidth()/5*3));
				holder.img_classif_one.setVisibility(View.VISIBLE);
				holder.img_classif_two.setVisibility(View.INVISIBLE);
			}else if(groupVo.artImg.size()>1){
				holder.img_classif_one.setVisibility(View.VISIBLE);
				holder.img_classif_two.setVisibility(View.VISIBLE);
			}
			if(groupVo.artLaber.size()>0){
				holder.grid_bqtype.setVisibility(View.VISIBLE);
				holder.grid_bqtype.setAdapter(new BtqianAdapter(activity, groupVo.artLaber));
			}else{
				holder.grid_bqtype.setVisibility(View.GONE);
			}
			view.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					Intent intent=new Intent(activity,WebActivity.class);
					intent.putExtra("isCollect", true);
					intent.putExtra("ID",groupVo.artId);
					intent.putExtra("NAME","文章内容详情");
					intent.putExtra("isCollect", groupVo.isCollect);
					activity.startActivity(intent);
				}
			});
			/** 文章赞的数量点击 */
			holder.ll_wenz_zan.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					httpPointPraiseArt(groupVo);
				}
			});
			/** 文章不喜欢的数量点击 */
			holder.ll_wenz_bi.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					httpPointNegativeArt(groupVo);
				}
			});
			/** 文章热度点击 */
			holder.ll_wenz_re.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
				}
			});
			/** 评论赞的数量点击 */
			holder.ll_pl_zan.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					httpPointPraiseDiscuss(groupVo);
				}
			});
			/** 收藏 */
			holder.img_shoucang.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
//					httpSetCollec(groupVo);
					Dialog(groupVo);
				}
			});
			/**
			 * 分享
			 */
			holder.img_share.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					Share.openShare(activity,"", "测试", "这只是个测试","http://www.baidu.com/" , "http://e.hiphotos.baidu.com/lvpics/h=800/sign=1d1cc1876a81800a71e5840e813533d6/5366d0160924ab185b6fd93f33fae6cd7b890bb8.jpg");
				}
			});
			/**
			 * 查看自己的信息
			 */
			holder.ll_class_user.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
//					StartIntent.startUser(activity,groupVo.);
				}
			});
			holder.btn_exit.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					Dialog(groupVo);
				}
			});
//			/**
//			 * 查看其他用户的信息
//			 */
//			holder.ll_class_usert.setOnClickListener(new OnClickListener() {
//				@Override
//				public void onClick(View arg0) {
//					StartIntent.startUsertwo(activity, groupVo.discussUser.userId);
//				}
//			});
		}
		return view;
	}

	class ViewHolder {
		/** 文章发表用户姓名 */
		TextView text_ification_username;
		/** 文章发表内容 */
		TextView text_ification_detail;
		/** 文章评论内容 */
		TextView text_pl_detail;
		/** 文章赞的数量 */
		TextView text_wenz_zan;
		/** 文章不喜欢的数量 */
		TextView text_wenz_bi;
		/** 文章热度 */
		TextView text_wenz_re;
		/** 评论赞的数量 */
		TextView text_pl_zan;
		/** 文章赞的数量点击 */
		LinearLayout ll_wenz_zan;
		/** 文章不喜欢的数量点击 */
		LinearLayout ll_wenz_bi;
		/** 文章热度点击 */
		LinearLayout ll_wenz_re;
		/** 评论赞的数量点击 */
		LinearLayout ll_pl_zan;
		/** 图片1 */
		ImageView img_classif_one;
		/** 图片2 */
		ImageView img_classif_two;
		/** 收藏 */
		ImageView img_shoucang;
		/** 赞 */
		ImageView img_zan_no;
		/** 差评 */
		ImageView img_bishi_no;
		/** 分享 */
		ImageView img_share;
		/** 评论人用户名 */
		TextView text_discussUser;
		/** 查看自己信息 */
		LinearLayout ll_class_user;
		/** 查看其他用户信息 */
		LinearLayout ll_class_usert;
		/** 关闭按钮 */
		Button btn_exit;
		GridView grid_bqtype;

		/** 文章用户头像*/
		RoundImageView img_userhead01;
		/** 精彩评论用户头像*/
		RoundImageView img_userhead02;
	}

	/**
	 * 文章差评接口
	 */
	private void httpPointNegativeArt(final ArtInfoVo groupVo) {
		HttpParams params = new HttpParams();
		params.put("artId", groupVo.artId);// 所属文章ID
		AppHttps.HttpPost(activity,HttpInterface.POINT_NEGATIVE_ART, params,
				new AppHttpCallBack<Object>(Object.class, activity) {
					@Override
					public void onSuccess(Object result) {
						List<ArtInfoVo> groupVos=getItems();
						for (int i = 0; i < groupVos.size(); i++) {
							if(groupVos.get(i).artId==groupVo.artId){
								groupVos.get(i).artNegativeNumber=groupVos.get(i).artNegativeNumber+1;
								groupVos.get(i).isArtNegativeNumber=true;
								break;
							}
						}
						setItems(groupVos);
						notifyDataSetChanged();
						ToastUtil.showShort(activity, "差评成功");
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(activity, errorMsg);
					}
				});
	}
	/**
	 * 文章取消差评接口
	 */
	private void httpPointExitNegativeArt(final artGroupVo groupVo) {
		HttpParams params = new HttpParams();
		params.put("artId", groupVo.artId);// 所属文章ID
		AppHttps.HttpPost(activity,HttpInterface.POINT_NEGATIVE_CANCEL_ART, params,
				new AppHttpCallBack<Object>(Object.class, activity) {
					@Override
					public void onSuccess(Object result) {
						List<artGroupVo> groupVos=getItems();
						for (int i = 0; i < groupVos.size(); i++) {
							if(groupVos.get(i).artId==groupVo.artId){
								groupVos.get(i).artNegativeNumber=groupVos.get(i).artNegativeNumber-1;
								groupVos.get(i).isNegative=false;
								break;
							}
						}
						setItems(groupVos);
						notifyDataSetChanged();
						ToastUtil.showShort(activity, "差评成功");
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(activity, errorMsg);
					}
				});
	}
	/**
	 * 对文章给予点赞功能
	 */
	private void httpPointPraiseArt(final ArtInfoVo groupVo) {
		HttpParams params = new HttpParams();
		params.put("artId", groupVo.artId);// 所属文章ID
		AppHttps.HttpPost(activity,HttpInterface.POINT_PRAISE_ART, params, new AppHttpCallBack<Object>(Object.class, activity) {
			@Override
			public void onSuccess(Object result) {
				List<ArtInfoVo> groupVos=getItems();
				for (int i = 0; i < groupVos.size(); i++) {
					if(groupVos.get(i).artId==groupVo.artId){
						groupVos.get(i).artPraiseNumber=groupVos.get(i).artPraiseNumber+1;
						groupVos.get(i).isArtPraiseNumber=true;
						break;
					}
				}
				setItems(groupVos);
				notifyDataSetChanged();
				ToastUtil.showShort(activity, "文章点赞成功");
			}

			@Override
			public void onFailures(int errorCode, String errorMsg) {
				ToastUtil.showShort(activity, errorMsg);
			}
		});
	}
	/**
	 * 对文章取消点赞功能
	 */
	private void httpPointExitPraiseArt(final artGroupVo groupVo) {
		HttpParams params = new HttpParams();
		params.put("artId", groupVo.artId);// 所属文章ID
		AppHttps.HttpPost(activity,HttpInterface.POINT_PRAISE_CANCEL_ART, params,
				new AppHttpCallBack<Object>(Object.class, activity) {
					@Override
					public void onSuccess(Object result) {
						List<artGroupVo> groupVos=getItems();
						for (int i = 0; i < groupVos.size(); i++) {
							if(groupVos.get(i).artId==groupVo.artId){
								groupVos.get(i).artPraiseNumber=groupVos.get(i).artPraiseNumber-1;
								groupVos.get(i).isPraise=false;
								break;
							}
						}
						setItems(groupVos);
						notifyDataSetChanged();
						ToastUtil.showShort(activity, "文章点赞成功");
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(activity, errorMsg);
					}
				});
	}

	/**
	 * 对文章进行收藏
	 */
	private void httpSetCollec(final ArtInfoVo groupVo) {
		HttpParams params = new HttpParams();
		params.put("artId", groupVo.artId);// 所属文章ID
		AppHttps.HttpPost(activity,HttpInterface.SET_COLLEC, params, new AppHttpCallBack<Object>(Object.class, activity) {
			@Override
			public void onSuccess(Object result) {
				List<artGroupVo> groupVos=getItems();
				for (int i = 0; i < groupVos.size(); i++) {
					if(groupVos.get(i).artId==groupVo.artId){
						groupVos.get(i).isCollect=true;
						break;
					}
				}
				setItems(groupVos);
				notifyDataSetChanged();
				ToastUtil.showShort(activity, "文章收藏成功");
			}

			@Override
			public void onFailures(int errorCode, String errorMsg) {
				ToastUtil.showShort(activity, errorMsg);
			}
		});
	}

	/**
	 * 对评论给予点赞功能
	 */
	private void httpPointPraiseDiscuss(final ArtInfoVo groupVo) {
		HttpParams params = new HttpParams();
//		params.put("artId", groupVo.artId);// 所属文章ID
		params.put("discussId", groupVo.discussId);// 所属评论ID
		AppHttps.HttpPost(activity,HttpInterface.POINT_PRAISE_DISCUSS, params,
				new AppHttpCallBack<Object>(Object.class, activity) {
					@Override
					public void onSuccess(Object result) {
						List<artGroupVo> groupVos=getItems();
						for (int i = 0; i < groupVos.size(); i++) {
							if(groupVos.get(i).artId==groupVo.artId){
								groupVos.get(i).discussPraiseNumber++;
								break;
							}
						}
						setItems(groupVos);
						notifyDataSetChanged();
						ToastUtil.showShort(activity, "文章取消收藏成功");
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(activity, errorMsg);
					}
				});
	}
	/**
	 * 取消收藏接口
	 */
	private void httpDelCollect(final ArtInfoVo groupVo) {
		HttpParams params = new HttpParams();
		params.put("artId", groupVo.artId);// 所属文章ID
		AppHttps.HttpPost(activity,HttpInterface.DEL_COLLECT, params,
				new AppHttpCallBack<Object>(Object.class, activity) {
					@Override
					public void onSuccess(Object result) {
						List<artGroupVo> groupVos=getItems();
						for (int i = 0; i < groupVos.size(); i++) {
							if(groupVos.get(i).artId==groupVo.artId){
								groupVos.get(i).isCollect=false;
								break;
							}
						}
						setItems(groupVos);
						notifyDataSetChanged();
						ToastUtil.showShort(activity, "文章取消收藏成功");
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(activity, errorMsg);
					}
				});
	}
	private void Dialog(final ArtInfoVo groupVo) {
		AlertDialog.Builder builder=new AlertDialog.Builder(activity);
		builder.setTitle("提示");
		builder.setMessage("取消收藏");
		builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				httpDelCollect(groupVo);

			}
		});
		builder.setNeutralButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
			}
		});
		builder.show();
	}

}
